import os
import time
from pprint import pprint
from tests.ry_roll_up_banner.common import constant
from tests.ry_roll_up_banner.common.util_common import *
import pymysql
import requests
import os

base_dir = str(os.path.dirname(os.path.dirname(__file__)))
base_dir = base_dir.replace('\\', '/')
print(base_dir)
# conf_path = base_dir + "/config/conf.ini"


host_url = constant.host_url
# 因为每次运行都要创建一个商场就很麻烦了，就指定一个商场
mallId = constant.mallId
mallName = constant.mallName
cityId = constant.cityId  # 上海
floorId = constant.floorId
layoutId_yilabao = constant.layoutId_yilabao  # 电子易拉宝的配置id
mall_groupId = constant.mall_groupId  # 该商场的素材分组-图片
mall_groupId_video = constant.mall_groupId_video  # 该商场的素材分组-视频
appcode_yilabao = constant.appcode_yilabao  # appcode  易拉宝是rollScreen  ，建站的是mallCustomized
user = constant.shop_userName  # 商家后台用户名


def mall_add_yilabao():
    print("为该商场配置电子易拉宝   " + str(mallId))
    header = {
        "Cookie": dayunying_login(),
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 "
                      "Safari/537.36"}
    url = host_url + "/easy-smart-basic/module/setmodulebylayout"
    data = {"layoutId": layoutId_yilabao, "mallId": mallId, "moduleIds": "601,602"}  # 600电子易拉宝的id
    r = requests.post(url, headers=header, json=data)
    print(r.status_code, r.json())
    result = r.json()
    assert result['meta']['msg'] == "success"


def query_case(casename):
    print("根据易拉宝的case名字获取caseId，模板case名是 " + str(casename))
    url = host_url + "/easy-smart-rollscreen/case/newQueryCase"
    header = {
        "Cookie": shop_login(),
        "Content-Type": 'application/json',
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
    }
    data = {"currentPage": 1, "mallId": mallId, "pageSize": 9, "style": "", "resolution": "",
            "auditState": "", "layoutId": layoutId_yilabao, "name": casename}
    r = requests.post(url=url, headers=header, json=data)
    caseId = r.json()['result']['data'][0]['id']
    print("根据casename获取到的caseId为    " + str(caseId))
    return caseId


def add_yilabao_case():
    print("添加易拉宝的模板case并获取caseId")
    # /easy-smart-rollscreen/case/checkCaseNameUsable  添加前会校验casename，重复的就不添加了
    # {"caseId":"","layoutId":"600","mallId":"5e928fa96bcb4a891c29bded","name":"taozhen"}
    url = host_url + "/easy-smart-rollscreen/caseLayerManage/addOrUpdate"
    header = {
        "Cookie": shop_login(),
        "Content-Type": 'application/json',
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
    }

    caseName = "自动化模板" + now
    data = {"caseId": "", "mallId": mallId, "caseLayerList": [
        {"type": 9, "top": 0, "left": 6.666666666666667, "width": 300, "height": 120, "radio": False, "move": True,
         "resize": True, "edit": [{"name": "删除", "type": "delete"}],
         "detailMap": {"text": "test", "color": "#000000", "fontSize": 24, "kerning": 2, "font": "1", "fontFamily": "1",
                       "spacing": 5, "transparency": 100, "bold": False, "underline": False, "shadow": False,
                       "alignLeft": "2", "italic": False}, "index": 1, "dragHeight": 120}], "caseName": caseName,
            "resolution": "1080*1920", "style": "3", "from": 3, "layoutId": layoutId_yilabao, "backgroundMusicUrl": "",
            "backgroundMusicVolume": 100}

    r = requests.post(url=url, headers=header, json=data)
    print(r.json())
    caseId = r.json()['result']['data']['caseId']
    print("添加易拉宝成功的  caseId为    " + str(caseId))
    return caseId


def delete_yilabao_case(caseId):
    print("删除易拉宝的模板caseId是   " + str(caseId))
    url = host_url + "/easy-smart-rollscreen/case/del?caseId=" + caseId + "&mallId=" + mallId
    header = {
        "Cookie": shop_login(),
        "Content-Type": 'application/json',
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
    }
    data = {}
    r = requests.get(url=url, headers=header, json=data)
    print(r.json())


def delete_yilabao_all_case():
    print("删除该商场下的所有易拉宝模板")
    url = host_url + "/easy-smart-rollscreen/case/newQueryCase"
    header = {
        "Cookie": shop_login(),
        "Content-Type": 'application/json',
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
    }
    data = {"currentPage": 1, "mallId": mallId, "pageSize": 9, "style": "", "resolution": "",
            "auditState": "", "layoutId": layoutId_yilabao, "name": ""}
    r = requests.post(url=url, headers=header, json=data)
    caseId_list = r.json()['result']['data']  # [0]['id']
    for case in caseId_list:
        caseId = case['id']
        print(caseId)
        delete_yilabao_case(str(caseId))
    print("删除该商场下的所有易拉宝模板 ---->    ok")


def upload_file(jpg_name):
    print("上传图片")
    # jpg_name = "1024-1024.jpg"
    photo_path = base_dir + "/data/" + jpg_name
    url = host_url + "/easy-smart/bsFileUpload/uploadFile"
    header = {
        # "Content-Type": 'multipart/form-data' ; boundary=----WebKitFormBoundaryfcQzWHVe71BEAzwk,  #Content-Type不能添加，添加就500
        # "X-Requested-With": "XMLHttpRequest"
    }
    image = {'file': (jpg_name, open(photo_path, 'rb'), 'image/jpeg')}
    print(image)
    r = requests.post(url=url, headers=header, files=image)
    print(r.json())
    assert r.json()['meta']['msg'] == "success"
    image_url = r.json()['result']['data'][0]['url']
    thumbnail_url = r.json()['result']['data'][0]['thumbnail']
    return thumbnail_url, image_url


def add_mall_image(jpg_name, size, attribute):
    image = upload_file(jpg_name)
    thumbnail_url = image[0]
    image_url = image[1]
    print("添加图片到商场里，图片链接是    " + str(image_url))
    url = host_url + "/easy-smart-basic/sourceManage/addBsSource"
    header = {
        "Cookie": shop_login(),
        "Content-Type": 'application/json',
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36"
    }
    data = {"mallId": mallId, "dataList": [{"thumbnail": thumbnail_url,
                                            "name": jpg_name,
                                            "url": image_url,
                                            "format": "jpg", "groupId": mall_groupId, "size": size,
                                            "type": 1, "attribute": attribute}]}
    r = requests.post(url=url, headers=header, json=data)
    print(r.json())
    assert r.json()['meta']['msg'] == "success"
    photo_id = r.json()['result']['data'][0]['id']
    print(photo_id, image_url, thumbnail_url, jpg_name, attribute)
    print("添加图片到商场成功")
    return photo_id, image_url, thumbnail_url, jpg_name, attribute


def upload_video(videoname="test.mp4"):
    print("上传视频  " + str(videoname))
    video_path = base_dir + "/data/" + videoname  # D:/pydj/ry_roll_up_banner
    url = host_url + "/easy-smart-basic/sourceManage/uploadVideo"
    header = {
        "Cookie": shop_login(),
    }
    # 遇到文件和参数都有的，那就文件传files   参数传data
    video = {'file': (videoname, open(video_path, 'rb'), 'video/mp4')}
    data = {"groupId": mall_groupId_video,
            "mallId": mallId}
    print(video)
    r = requests.post(url=url, headers=header, data=data, files=video)
    print(r.json())
    assert r.json()['meta']['msg'] == "success"


def get_video_msg(videoname):
    upload_video(videoname)
    print("获取上传视频的id、mp4链接、名字、分辨率、预览图    " + str(videoname))
    url = host_url + "/easy-smart-basic/sourceManage/query"
    header = {
        "Cookie": shop_login(),
        "Content-Type": 'application/json',
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36"
    }
    data = {"mallId": mallId, "groupId": mall_groupId_video, "currentPage": 1, "pageSize": 17, "name": "", "type": 2}
    r = requests.post(url=url, headers=header, json=data)
    print(r.json())
    assert r.json()['meta']['msg'] == "success"
    video_data = r.json()['result']['data'][0]
    video_id = video_data['id']
    mp4_url = video_data['url']
    attribute = video_data['attribute']
    preview_photo = video_data['preview']
    originalSizePreview_photo = video_data['originalSizePreview']
    print(video_id, mp4_url, videoname, attribute, preview_photo, originalSizePreview_photo)
    return video_id, mp4_url, videoname, attribute, preview_photo, originalSizePreview_photo


def w_mp4(mp4_url):
    # mp4_url 视频url连接
    resp = requests.get(mp4_url)
    content = resp.content
    with open("111.mp4", "wb") as f:
        f.write(content)


# mall_add_yilabao()
# query_case("123")
# add_yilabao_case()
# delete_yilabao_case("3435")
# img = upload_file()
# add_mall_image(img)
# delete_yilabao_all_case()

# upload_file()

# upload_video()
# add_mall_image(jpg_name="1024-1024.jpg", size=239657, attribute="1024*1024")
# get_video_msg(videoname="test.mp4")


def add_yilabao_case_full():
    dantu1024 = add_mall_image(jpg_name="1024-1024.jpg", size=239657, attribute="1024*1024")
    lunbo_1 = add_mall_image(jpg_name="lunbo-1.jpg", size=595284, attribute="1024*768")
    lunbo_2 = add_mall_image(jpg_name="lunbo-2.jpg", size=51780, attribute="220*226")
    test = get_video_msg(videoname="test.mp4")
    lunbo_MP4 = get_video_msg(videoname="lunbo-MP4.mp4")

    print("添加易拉宝的模板case并获取caseId")
    url = host_url + "/easy-smart-rollscreen/caseLayerManage/addOrUpdate"
    header = {
        "Cookie": shop_login(),
        "Content-Type": 'application/json',
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
    }

    caseName = "自动化模板" + now
    data = {
        "caseId": "",
        "mallId": mallId,
        "caseLayerList": [{
            "type": 8,
            "top": 0,
            "left": 0,
            "width": 627.5862068965517,
            "height": 631.0344827586207,
            "radio": False,
            "move": True,
            "resize": True,
            "edit": [{
                "name": "删除",
                "type": "delete"
            }],
            "detailMap": {
                "playEffect": "1"
            },
            "single": True,
            "dataList": [{
                "id": dantu1024[0],
                "mallId": mallId,
                "shopId": "",
                "url": dantu1024[1],
                "thumbnail": dantu1024[2],
                "name": dantu1024[3],
                "state": 1,
                "type": 1,
                "size": 0,
                "groupId": mall_groupId,
                "createAt": 1588919496,
                "updateAt": 1588919496,
                "createBy": user,
                "updateBy": user,
                "module": "",
                "attribute": dantu1024[4],
                "token": "",
                "format": "",
                "isPublic": 0,
                "preview": "",
                "tagId": "",
                "detail": "",
                "layerList": "",
                "top": "",
                "left": "",
                "width": "",
                "height": "",
                "sourceTop": "",
                "sourceLeft": "",
                "originalSizePreview": "",
                "selected": True,
                "sourceId": dantu1024[0]
            }],
            "index": 1,
            "hasPic": True,
            "hasVideo": False,
            "interval": 0,
            "dragHeight": 631.0344827586207,
            "sourceRefList": [{
                "id": dantu1024[0],
                "mallId": mallId,
                "shopId": "",
                "url": dantu1024[1],
                "thumbnail": dantu1024[2],
                "name": dantu1024[3],
                "state": 1,
                "type": 1,
                "size": 0,
                "groupId": mall_groupId,
                "createAt": 1588919496,
                "updateAt": 1588919496,
                "createBy": user,
                "updateBy": user,
                "module": "",
                "attribute": dantu1024[4],
                "token": "",
                "format": "",
                "isPublic": 0,
                "preview": "",
                "tagId": "",
                "detail": "",
                "layerList": "",
                "top": "",
                "left": "",
                "width": "",
                "height": "",
                "sourceTop": "",
                "sourceLeft": "",
                "originalSizePreview": "",
                "selected": True,
                "sourceId": dantu1024[0]
            }]
        }, {
            "type": 9,
            "top": 0,
            "left": 627.5862068965517,
            "width": 441.3793103448276,
            "height": 251.7241379310345,
            "radio": False,
            "move": True,
            "resize": True,
            "edit": [{
                "name": "删除",
                "type": "delete"
            }],
            "detailMap": {
                "text": "测试文字",
                "color": "#ca4176",
                "fontSize": 50,
                "kerning": 2,
                "font": "1",
                "fontFamily": "1",
                "spacing": 5,
                "transparency": 100,
                "bold": True,
                "underline": False,
                "shadow": False,
                "alignLeft": "2",
                "italic": False
            },
            "index": 2,
            "dragHeight": 251.7241379310345
        }, {
            "type": 12,
            "top": 410.3448275862069,
            "left": 658.6206896551724,
            "width": 379.3103448275862,
            "height": 179.31034482758622,
            "radio": False,
            "move": True,
            "resize": True,
            "edit": [{
                "name": "删除",
                "type": "delete"
            }],
            "detailMap": {
                "text": "跳转按钮文字",
                "color": "#1890FF",
                "fontSize": 45,
                "kerning": 2,
                "font": "1",
                "fontFamily": "1",
                "spacing": 4,
                "transparency": 100,
                "bold": False,
                "underline": False,
                "shadow": False,
                "alignLeft": "2",
                "italic": False,
                "background": "#E6F7FF",
                "borderRadius": 50,
                "bgimg": ""
            },
            "index": 3,
            "dragHeight": 179.31034482758622,
            "moduleId": 700
        }, {
            "type": 4,
            "top": 658.6206896551724,
            "left": 13.793103448275863,
            "width": 601,
            "height": 190,
            "radio": False,
            "move": True,
            "resize": False,
            "edit": [{
                "name": "删除",
                "type": "delete"
            }],
            "index": 4,
            "detailMap": {
                "is24Hour": True,
                "selectedWeatherStyle": 1,
                "selectedWeatherTheme": 8,
                "selectedDateFormat": 1,
                "selectedTimeFormat": 1,
                "menuOpen": False,
                "strongColor": "#4A89DC",
                "weakColor": "#A0C8FB",
                "dateFormat": "YYYY-MM-DD",
                "timeFormat": "HH:mm:ss"
            },
            "dragHeight": 190
        }, {
            "type": 5,
            "top": 879.3103448275863,
            "left": 0,
            "width": 1068.9655172413793,
            "height": 244.82758620689657,
            "radio": False,
            "move": True,
            "resize": True,
            "edit": [{
                "name": "删除",
                "type": "delete"
            }],
            "index": 6,
            "detailMap": {
                "url": "http://www.baidu.com"
            },
            "dragHeight": 244.82758620689657
        }, {
            "type": 8,
            "top": 1162.0689655172414,
            "left": 0,
            "width": 437.93103448275866,
            "height": 755.1724137931035,
            "radio": False,
            "move": True,
            "resize": True,
            "edit": [{
                "name": "删除",
                "type": "delete"
            }],
            "detailMap": {
                "playEffect": "1"
            },
            "single": True,
            "dataList": [{
                "id": test[0],
                "mallId": mallId,
                "shopId": "",
                "url": test[1],
                "thumbnail": "",
                "name": test[2],
                "state": 1,
                "type": 2,
                "size": 1,
                "groupId": mall_groupId_video,
                "createAt": 1588919953,
                "updateAt": 1588919953,
                "createBy": user,
                "updateBy": user,
                "module": 1,
                "attribute": test[3],
                "token": "",
                "format": "",
                "isPublic": 0,
                "preview": test[4],
                "tagId": "",
                "detail": "",
                "layerList": "",
                "top": "",
                "left": "",
                "width": "",
                "height": "",
                "sourceTop": "",
                "sourceLeft": "",
                "originalSizePreview": test[5],
                "selected": True,
                "sourceId": test[0]
            }],
            "index": 8,
            "hasPic": False,
            "hasVideo": True,
            "interval": 0,
            "dragHeight": 755.1724137931035,
            "sourceRefList": [{
                "id": test[0],
                "mallId": mallId,
                "shopId": "",
                "url": test[1],
                "thumbnail": "",
                "name": test[2],
                "state": 1,
                "type": 2,
                "size": 1,
                "groupId": mall_groupId_video,
                "createAt": 1588919953,
                "updateAt": 1588919953,
                "createBy": user,
                "updateBy": user,
                "module": 1,
                "attribute": test[3],
                "token": "",
                "format": "",
                "isPublic": 0,
                "preview": test[4],
                "tagId": "",
                "detail": "",
                "layerList": "",
                "top": "",
                "left": "",
                "width": "",
                "height": "",
                "sourceTop": "",
                "sourceLeft": "",
                "originalSizePreview": test[5],
                "selected": True,
                "sourceId": test[0]
            }]
        }, {
            "type": 8,
            "top": 1162.0689655172414,
            "left": 500.00000000000006,
            "width": 427.58620689655174,
            "height": 755.1724137931035,
            "radio": False,
            "move": True,
            "resize": True,
            "edit": [{
                "name": "删除",
                "type": "delete"
            }],
            "detailMap": {
                "playEffect": "1"
            },
            "index": 9,
            "hasPic": True,
            "hasVideo": True,
            "dataList": [{
                "id": lunbo_MP4[0],
                "mallId": mallId,
                "shopId": "",
                "url": lunbo_MP4[1],
                "thumbnail": "",
                "name": lunbo_MP4[2],
                "state": 1,
                "type": 2,
                "size": 1,
                "groupId": mall_groupId_video,
                "createAt": 1588935475,
                "updateAt": 1588935475,
                "createBy": user,
                "updateBy": user,
                "module": 1,
                "attribute": lunbo_MP4[3],
                "token": "",
                "format": "",
                "isPublic": 0,
                "preview": lunbo_MP4[4],
                "tagId": "",
                "detail": "",
                "layerList": "",
                "top": "",
                "left": "",
                "width": "",
                "height": "",
                "sourceTop": "",
                "sourceLeft": "",
                "originalSizePreview": lunbo_MP4[5],
                "selected": True,
                "sourceId": lunbo_MP4[0]
            }, {
                "id": lunbo_1[0],
                "mallId": mallId,
                "shopId": "",
                "url": lunbo_1[1],
                "thumbnail": lunbo_1[2],
                "name": lunbo_1[3],
                "state": 1,
                "type": 1,
                "size": 1,
                "groupId": mall_groupId,
                "createAt": 1589026297,
                "updateAt": 1589026297,
                "createBy": user,
                "updateBy": user,
                "module": "",
                "attribute": lunbo_1[4],
                "token": "",
                "format": "",
                "isPublic": 0,
                "preview": "",
                "tagId": "",
                "detail": "",
                "layerList": "",
                "top": "",
                "left": "",
                "width": "",
                "height": "",
                "sourceTop": "",
                "sourceLeft": "",
                "originalSizePreview": "",
                "selected": True,
                "sourceId": lunbo_1[0]
            }, {
                "id": lunbo_2[0],
                "mallId": mallId,
                "shopId": "",
                "url": lunbo_2[1],
                "thumbnail": lunbo_2[2],
                "name": lunbo_2[3],
                "state": 1,
                "type": 1,
                "size": 0,
                "groupId": mall_groupId,
                "createAt": 1589026297,
                "updateAt": 1589026297,
                "createBy": user,
                "updateBy": user,
                "module": "",
                "attribute": lunbo_2[4],
                "token": "",
                "format": "",
                "isPublic": 0,
                "preview": "",
                "tagId": "",
                "detail": "",
                "layerList": "",
                "top": "",
                "left": "",
                "width": "",
                "height": "",
                "sourceTop": "",
                "sourceLeft": "",
                "originalSizePreview": "",
                "selected": True,
                "sourceId": lunbo_2[0]
            }],
            "interval": 5,
            "dragHeight": 755.1724137931035,
            "dataListBeforeIndex": 2,
            "dataListToIndex": 0,
            "sourceRefList": [{
                "id": lunbo_MP4[0],
                "mallId": mallId,
                "shopId": "",
                "url": lunbo_MP4[1],
                "thumbnail": "",
                "name": lunbo_MP4[2],
                "state": 1,
                "type": 2,
                "size": 1,
                "groupId": mall_groupId_video,
                "createAt": 1588935475,
                "updateAt": 1588935475,
                "createBy": user,
                "updateBy": user,
                "module": 1,
                "attribute": lunbo_MP4[3],
                "token": "",
                "format": "",
                "isPublic": 0,
                "preview": lunbo_MP4[4],
                "tagId": "",
                "detail": "",
                "layerList": "",
                "top": "",
                "left": "",
                "width": "",
                "height": "",
                "sourceTop": "",
                "sourceLeft": "",
                "originalSizePreview": lunbo_MP4[5],
                "selected": True,
                "sourceId": lunbo_MP4[0]
            }, {
                "id": lunbo_1[0],
                "mallId": mallId,
                "shopId": "",
                "url": lunbo_1[1],
                "thumbnail": lunbo_1[2],
                "name": lunbo_1[3],
                "state": 1,
                "type": 1,
                "size": 1,
                "groupId": mall_groupId,
                "createAt": 1589026297,
                "updateAt": 1589026297,
                "createBy": user,
                "updateBy": user,
                "module": "",
                "attribute": lunbo_1[4],
                "token": "",
                "format": "",
                "isPublic": 0,
                "preview": "",
                "tagId": "",
                "detail": "",
                "layerList": "",
                "top": "",
                "left": "",
                "width": "",
                "height": "",
                "sourceTop": "",
                "sourceLeft": "",
                "originalSizePreview": "",
                "selected": True,
                "sourceId": lunbo_1[0]
            }, {
                "id": lunbo_2[0],
                "mallId": mallId,
                "shopId": "",
                "url": lunbo_2[1],
                "thumbnail": lunbo_2[2],
                "name": lunbo_2[3],
                "state": 1,
                "type": 1,
                "size": 0,
                "groupId": mall_groupId,
                "createAt": 1589026297,
                "updateAt": 1589026297,
                "createBy": user,
                "updateBy": user,
                "module": "",
                "attribute": lunbo_2[4],
                "token": "",
                "format": "",
                "isPublic": 0,
                "preview": "",
                "tagId": "",
                "detail": "",
                "layerList": "",
                "top": "",
                "left": "",
                "width": "",
                "height": "",
                "sourceTop": "",
                "sourceLeft": "",
                "originalSizePreview": "",
                "selected": True,
                "sourceId": lunbo_2[0]
            }]
        }],
        "caseName": caseName,
        "resolution": "1080*1920",
        "style": "3",
        "from": 3,
        "layoutId": layoutId_yilabao,
        "backgroundMusicUrl": "",
        "backgroundMusicVolume": 100
    }

    r = requests.post(url=url, headers=header, json=data)
    print(r.json())
    caseId = r.json()['result']['data']['caseId']
    print("添加易拉宝成功的  caseId为    " + str(caseId))
    return caseId



# add_yilabao_case_full()